import { AssetManager } from "cc";
import { GameCore } from "../../../Base/Class";
import { setClassName } from "../../../Base/Global";
import { Singleton } from "../../../Base/Singleton";
import { AssetInfo } from "../base/AssetInfo";
import { ResourceLoadSpace } from "../_h/Index";
import { AssetCollectionLoadSystem } from "./AssetCollectionLoadSystem";

type AssetCollectionCallBack = (cacheData : any, collectionInfo : AssetCollectionInfo)=>void;
export class AssetCollectionInfo extends GameCore.ClassBase implements ResourceLoadSpace.IAssetCollectionInfo {
    public assetInfos : Array<AssetInfo> = new Array<AssetInfo>();
    private loadAssetMap : Map<number, AssetManager.Bundle> = new Map<number, AssetManager.Bundle>();
    public callBack : AssetCollectionCallBack = null;
    public callBackCacheData : any = null;
    private isLoadDone : boolean = false;
    public AddAssetInfo(assetInfo : AssetInfo)
    {
        this.assetInfos.push(assetInfo);
    }
    public AddLoadDoneCallBack(func : AssetCollectionCallBack, cacheData : any)
    {
        this.callBack=func;
        this.callBackCacheData = cacheData;
    }
    public StartRequestAsset()
    {
        Singleton.Instance(AssetCollectionLoadSystem).AddAssetCollection(this);
    }
    
    public SetAsset(assetInfo : AssetInfo, asset : AssetManager.Bundle)
    {
        this.loadAssetMap.set(assetInfo.instanceId, asset);
    }

    public GetAsset(assetInfo : AssetInfo) : AssetManager.Bundle
    {
        return this.loadAssetMap.get(assetInfo.instanceId);
    }

    public SetLoadDoneState()
    {
        this.isLoadDone = true;
        if(this.callBack != null)
        {
            this.callBack(this.callBackCacheData, this);
        }
    }

    public get IsDone():boolean {return this.isLoadDone;}

    public Release()
    {
        this.callBack = null;
        this.callBackCacheData = null;
        this.assetInfos.slice();
        this.loadAssetMap.clear();
        this.isLoadDone = false;
    }
}
setClassName(AssetCollectionInfo, "AssetCollectionInfo")